<?php
/**
 * Created by PhpStorm.
 * 商家处理业务
 * User: caicm
 * Date: 2017/6/15
 * Time: 12:02
 */

namespace WMApi\Service;
use WMApi\Model\WmSellerModel;
use WMApi\Model\UsersModel;

class SellerService {

    /**
     * 商家注册操作
     * Created By caicm
     * @param string $username
     * @param string $password
     * @param string $email
     * @param string $ec_salt
     * @param int $mobile_phone
     * @param string $seller_token
     * @return bool|mixed
     */
    public function reg_seller($username='', $password='', $email='', $ec_salt='', $mobile_phone=0, $seller_token=''){
        $seller_model = new WmSellerModel();
        $users_model = new UsersModel();
        $users_model->startTrans();

        /**
         * 写入用户表
         */
        $user_id = $users_model->add_users($username, $password, $email, $ec_salt, $mobile_phone);

        //查询商家表是否有写入
        $seller_info = $seller_model->find_seller_info(0, $user_id);
        if($seller_info){
            return false;
        }

        /**
         * 写入商家表
         */
        $seller_id = $seller_model->add_seller($user_id, $seller_token);

        if($user_id && $seller_id){
            $users_model->commit();
            return $seller_id;
        }else{
            //回滚
            $users_model->rollback();
            return false;
        }

    }
}